package com.lhj.bootdemo.thread;

/**
 * @author lhj
 * @version 1.0
 * @date 2022/4/27 2:48 下午
 * @description: 线程停止的demo
 */

public class ThreadStopDemo {

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
            while (num <= 10000 && !Thread.currentThread().isInterrupted()) {
                try {
                    if (num % 100 == 0) {
                        System.out.println(num + "是100的倍数");
                    }
                    num++;
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5000);
        //通过 interrupt方法 来中断线程
        thread.interrupt();
    }
}
